package com.jit.monitor.presenter;

import android.annotation.SuppressLint;
import android.app.Application;
import android.util.Log;

import com.jit.baselib.bean.response.BaseListResponse;
import com.jit.baselib.bean.response.BaseResponse;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BaseObserver;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.baselib.ui.base.ExceptionHandle;
import com.jit.monitor.base.ApiRetrofit;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.bean.EarlyWarning;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.model.request.WarnRequest;
import com.jit.monitor.model.response.MainResponse;
import com.jit.monitor.util.JPushHelper;
import com.jit.monitor.view.MainAtView;


import java.nio.channels.ShutdownChannelGroupException;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.service.AlarmReceiver;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.operators.observable.ObservableNever;
import io.reactivex.schedulers.Schedulers;

/**
 * @author crazyZhangxl on 2018/12/21.
 * Describe:
 */
public class MainAtPresenter extends BasePresenter<MainAtView>{
    public MainAtPresenter(BaseActivity context) {
        super(context);
    }

    @SuppressLint("CheckResult")
    public void doLogout(){
        getView().showLoadingDialog();
        Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
            UserCache.clear();
            emitter.onNext(true);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Boolean>() {
                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        MainAtPresenter.this.getView().hideLoadingDialog();
                        MainAtPresenter.this.getView().logoutFailure();
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        MainAtPresenter.this.getView().hideLoadingDialog();
                        if (aBoolean) {
                            MainAtPresenter.this.getView().logoutSuccess();
                            JPushHelper.getInstance().removeAlias();
                        } else {
                            MainAtPresenter.this.getView().logoutFailure();
                        }
                    }
                });
    }

    @SuppressLint("CheckResult")
    public void queryList(int page) {
        ApiRetrofit.getInstance().getCameraAndCurrentFrameInfo(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<MainResponse>>() {
                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().queryListFailure(exception.getErrorInfo());
                    }

                    @Override
                    public void onNext(BaseListResponse<MainResponse> mainResponseBaseListResponse) {
                        if (mainResponseBaseListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            getView().queryListSuccess(mainResponseBaseListResponse.getData());
                        }else {
                            getView().queryListFailure(mainResponseBaseListResponse.getMsg());
                        }
                    }
                });
    }

    public void queryType(){
        ApiRetrofit.getInstance().queryAlarmRing()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<Integer>>() {
                    @Override
                    public void onNext(BaseResponse<Integer> integerBaseResponse) {
                        if (integerBaseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (integerBaseResponse.getData() == null){
                                getView().queryTypeSuccess(AppConstant.NOTIFICATION_TYPE);
                            }else {
                                getView().queryTypeSuccess(integerBaseResponse.getData());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().queryTypeFailure(exception.getErrorInfo());
                    }
                });


    }


    public void setAlarmType(int type, String name){
        ApiRetrofit.getInstance().setAlarmRing(UserCache.getUserID(),type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            getView().setTypeSuccess(type,name);
                        }else {
                            getView().setTypeFailure(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().setTypeFailure(exception.getErrorInfo());
                    }
                });
    }

    /**
     *
     * @param position 操作的是哪一个item
     * @param cameraID 操作的是那个id
     * @param flag     操作值
     */
    public void userControlCamera(int position,String cameraName,String cameraID,String flag){
        ApiRetrofit.getInstance().controlNotificationByInfo(cameraID,flag)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                            String des = AppConstant.NOTIFY_WITH_LED_ON.equals(flag) ? "开启" : "关闭";
                            getView().controlNotifySuccess(des+"摄像头名称为 "+cameraName+"预警通知成功!");
                        }else {
                            getView().controlNotifyFailure(position,flag,"预警通知操作失败:"+baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().controlNotifyFailure(position,flag,"预警通知操作失败:"+exception.getErrorInfo());
                    }
                });
    }

    /**
     * 这里
     * @param position
     * @param cameraID
     */
    public void userControlLed(int position,String cameraID){
        ApiRetrofit.getInstance().controlLedState(cameraID,AppConstant.NOTIFY_WITH_LED_OFF)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            getView().controlLedSuccess(position,"关闭LED灯成功");
                        }else {
                            getView().controlLedFailure(position,baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().controlLedFailure(position,exception.getErrorInfo());
                    }
                });

    }
}
